package lancs.mobilemedia.core.ui.controller.commands;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;

import lancs.mobilemedia.core.ui.controller.AbstractController;
import lancs.mobilemedia.core.ui.controller.ControllerCommand;
import lancs.mobilemedia.core.ui.controller.PhotoController;
import lancs.mobilemedia.core.ui.datamodel.AlbumData;
import lancs.mobilemedia.core.ui.screens.NewLabelScreen;
import lancs.mobilemedia.lib.exceptions.ImageNotFoundException;
import lancs.mobilemedia.lib.exceptions.NullAlbumDataReference;


public class EditLabel implements ControllerCommand {

	public boolean execute(AbstractController controller) {
		String selectedImageName = ((PhotoController) controller).getSelectedImageName();
		try {
			((PhotoController) controller).updateImage(controller.getAlbumData().getImageInfo(selectedImageName));
			
			NewLabelScreen formScreen = new NewLabelScreen("Edit Label Photo", NewLabelScreen.LABEL_PHOTO);
			formScreen.setCommandListener(controller);
			((PhotoController) controller).setScreen(formScreen);
			((PhotoController) controller).setCurrentScreen(formScreen);
			formScreen = null;
		} catch (ImageNotFoundException e) {
			Alert alert = new Alert("Error", "The selected photo was not found in the mobile device",
					null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert,
					Display.getDisplay(controller.getMidlet()).getCurrent());
		} catch (NullAlbumDataReference e) {
			controller.setAlbumData( new AlbumData() );
			Alert alert = new Alert( "Error", "The operation is not available. Try again later !", null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert, Display.getDisplay(controller.getMidlet()).getCurrent());
		}
		return true;
	}
}
